package cn.itsource.pet.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return searchMasterMsgService.getAll();
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){

        //获取当前登录信息要在contorller中，只有controller中才能获取request
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        try {

            searchMasterMsgService.publish(searchMasterMsg,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("发布失败！"+e.getMessage());
        }
    }


    //待处理消息-平台管理员或店铺管理员（员工）
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        return searchMasterMsgService.pending(query,loginInfo.getId());
    }


    //已处理消息-平台管理员或店铺管理员（员工）
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        return searchMasterMsgService.processed(query,loginInfo.getId());
    }

    //查看用户发布消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> query(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        return searchMasterMsgService.query(query,loginInfo.getId());
    }
    //消息处理
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            Long loginInfoId = LoginContext.getLoginInfo(request).getId();
            searchMasterMsgService.handle(pet,loginInfoId);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("寻主消息处理失败！"+e.getMessage());
        }
    }

}
